<template>
  <div class="_wz_cent_main _wz_hiddenTroubleplan">
    <Card dis-hover :bordered="true" style="margin:0 0 10px;">
      <p slot="title">搜索</p>
      <div slot="extra">
        <Button type="info" size="small" v-if="!searchType" @click="searchType = true" icon="ios-arrow-down">更多</Button>
        <Button type="info" size="small" v-else @click="searchType = false" icon="ios-arrow-up">收起</Button>
      </div>
      <Form :model="formDataSearch" label-position="right" :label-width="80" inline>
        <FormItem label="任务名称">
          <i-input v-model="formDataSearch.planName" placeholder="请填写任务名称" clearable />
        </FormItem>
        <FormItem label="排查类型">
          <i-select v-model="formDataSearch.inspectTypeUuid" filterable placeholder="请选择排查类型" clearable>
            <Option v-for="(item,index) in inspectList" :value="item.uuid" :key="item.index">{{ item.typeName }}</Option>
          </i-select>
        </FormItem>
        <FormItem label="计划状态" v-if="searchType">
          <i-select v-model="formDataSearch.status" filterable placeholder="请选择计划状态" clearable>
            <Option value="0">未发布</Option>
            <Option value="1">已发布</Option>
            <Option value="2">已完成</Option>
          </i-select>
        </FormItem>

        <FormItem label="运输企业" v-if="searchType">
          <i-select v-model="formDataSearch.deptUuid" filterable placeholder="请选择运输企业" clearable>
            <Option v-for="item in subDeptList" :value="item.uuid" :key="item.uuid">{{ item.deptName }}</Option>
          </i-select>
        </FormItem>

        <FormItem label="日期检索" v-if="searchType">
          <DatePicker type="daterange" v-model="dateRange" placeholder="请选择日期范围" style="width:96%"></DatePicker>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="searchSubmit">搜索</Button>
        </FormItem>
      </Form>
    </Card>
    <Card dis-hover :bordered="true">
      <p slot="title">列表</p>
      <p slot="extra">
        <Button type="primary" @click="addTableData" v-if="pageType == 2">添加</Button>
      </p>
      <wzTbale ref="wzTbale" :tableData="tableData"></wzTbale>
    </Card>
    <!--    :mask-closable="false" :footer-hide="true"-->
    <Modal :title="formTitle" v-model="formOperation" width="600" :mask-closable="false" :footer-hide="true" >
      <hiddenTroubleplanAdd v-if="formOperation" @closeFun="closeFun" :selectData="selectData"></hiddenTroubleplanAdd>
    </Modal>
    <Drawer title="排查点" v-model="seeMoreType" width="800" :mask-closable="false" :draggable="true">
      <risksPlan v-if="seeMoreType" :selectDataParent="selectData"  @closeFun="closeFun"></risksPlan>
    </Drawer>



    <Drawer title="详情" v-model="screeningPlanDetailsType" width="800" :mask-closable="false" :draggable="true">
      <screeningPlanDetails v-if="screeningPlanDetailsType" :selectData="selectData" :pageType="pageType" @hiddenPerils="hiddenPerils"></screeningPlanDetails>
    </Drawer>


  </div>
</template>
<script>


import hiddenTroubleplanAdd from "./hiddenTroubleplanAdd";
import risksPlan from "./risksPlan";
import wzTbale from "../../components/table/table";
import screeningPlanDetails from "./screeningPlanDetails";



export default {
  //pageType  1 执行计划    2 排查计划
  props: ['pageType'],//接收来自父组件的数据
  components: {
    hiddenTroubleplanAdd,//添加
    risksPlan,//查看详情
    wzTbale,//列表
    screeningPlanDetails,//排查计划详情
  },
  data() {
    return {

      dateRange: [],//时间范围`
      searchType:false,//搜索条件
      inspectList:[],
      screeningPlanDetailsType:false,
      subDeptList: [],//运输企业
      seeMoreType: false,
      formDataSearch: {},//搜索表单数据
      formTitle: '',
      selectData: {},
      formOperation: false,//表单左侧移出
      formOperationStyles: {//表单左侧移出css
        height: 'calc(100% - 55px)',
        overflow: 'auto',
        paddingBottom: '53px',
        position: 'static'
      },
      tableData: {
        url: this.apiUrl.webApi + (this.pageType == 1?'/safety/risk/riskPlanInspectPageList':'/safety/risk/riskPlanPageList'),//请求地址
        method: 'post',//请求方式
        where: {},//搜索条件
        height: 'auto',//表格高度 auto
        page: true,//分页是否使用
        columns: [//表头数据
          {type: 'index', width: 70, align: 'center', title: '序号'},
          {title: '任务名称', minWidth:220, key: 'name'},
          {title: '排查类型', minWidth:140, key: 'inspectTypeName'},
          {title: '开始时间', minWidth: 160, key: 'startDate' },
          {title: '结束时间', minWidth: 160,  key:'endDate' },
          {title: '运输企业', minWidth: 260, key: 'deptName'},
          {title: '计划状态', minWidth: 160,  render: (h, params) => {
              //  "计划状态（0 草稿、1 发布、2 完成）"
              var test = '';
              if (params.row.status == 0) {
                test = h('Tag', {props: {type: 'dot', color: 'warning'}}, '未发布')
              } else if (params.row.status == 1) {
                test = h('Tag', {props: {type: 'dot', color: 'primary'}}, '已发布')
              } else if (params.row.status == 2) {
                test = h('Tag', {props: {type: 'dot', color: 'success'}}, '已完成')
              }
              return test;
            }
          },
          {
            title: '操作', fixed: 'right', width:120,
            render: (h, params) => {
              let that = this;
              var operationButtons = [];
              operationButtons.push(
                h('span', {
                  style: {"color": "#2d8cf0", "cursor": "pointer"},
                  on: {
                    click: () => {
                      this.selectData = params.row;
                      this.screeningPlanDetailsType = true;
                    }
                  }
                },((that.pageType == 1 && (new Date(params.row.startDate).getTime() < new Date().getTime()) && (new Date().getTime() < new Date(params.row.endDate).getTime()))?'执行':'查看') )
              )

              var buttonList = [];

              if(params.row.status == 0){
                //排查项
                buttonList.push(h('DropdownItem', {
                  nativeOn: {
                    click: () => {
                      this.selectData = params.row;
                      this.seeMoreType = true;
                    }
                  }
                }, '排查点'))


                //发布
                buttonList.push(h('DropdownItem', {
                  nativeOn: {
                    click: () => {
                      let that = this;
                      that.$Modal.confirm({
                        title: '发布提醒',
                        content: '确定要发布选中的记录？',
                        onOk: () => {
                          that.spinShow = true;
                          var data =  JSON.parse(JSON.stringify(params.row));
                          data.status = 1;
                          that.axios.post(that.apiUrl.webApi + "/safety/risk/riskPlanSaveOrUpdate", data).then(res => {
                            that.spinShow = false;
                            if (!!res) {
                              that.$Message.success('操作成功.');
                              that.$refs.wzTbale.tableLoadData();
                            }
                          }).catch(err => {
                            console.log("失败", err)
                          })
                        },
                        onCancel: () => {
                          //点击取消
                        }
                      });
                    }
                  }
                }, '发布'))


                //修改
                buttonList.push(h('DropdownItem', {
                  nativeOn: {
                    click: () => {
                      this.formTitle = '修改';
                      this.selectData = JSON.parse(JSON.stringify(params.row));
                      this.formOperation = true;
                    }
                  }
                }, '修改'))
                //删除
                buttonList.push(h('DropdownItem', {
                  nativeOn: {
                    click: () => {
                      this.removeTableFun(params.row);
                    }
                  }
                }, '删除'))
              }

              if (buttonList.length > 0) {
                if(operationButtons.length > 0){
                  operationButtons.push(h('Divider', {props: {type: 'vertical',}}, ''))
                }
                operationButtons.push(h('Dropdown', {props: {trigger: "hover", transfer: true}},
                  [
                    h('span', {style: {"color": "#2d8cf0", "cursor": "pointer"}}, '更多▼'),
                    h('DropdownMenu', {slot: "list"}, buttonList)
                  ])
                )
              }


              return operationButtons;
            }
          },
        ],
        format: {//返回数据格式
          list: ['data'],//数据list
          count: ['count'],//数据总量
        },
      },

    }
  },
  beforeCreate() {//beforeCreate创建前状态

  },
  created() {//created创建完毕状态
  },
  beforeMount() {//beforeMount挂载前状态   DOM 元素然后塞入页面的过程

  },
  mounted() {//mounted 挂载结束状态
    var that = this;

    //隐患排查类型
    that.axios.post(that.apiUrl.webApi + "/safety/risk/riskInspectTypeSettingList", {}).then(res => {
      if (!!res) {
        that.inspectList = res.data.data;
      }
    }).catch(err => {
      console.log("失败", err)
    })

    //运输企业
    that.axios.post(that.apiUrl.coreDeptCoreDeptList,{}).then(res => {
      if (!!res) {
        that.subDeptList = res.data.data;
      }
    }).catch(err => {
      console.log("失败", err)
    })

  },
  methods: {//执行的方法
    removeTableFun(data) {//删除
      let that = this;
      that.$Modal.confirm({
        title: '删除提醒',
        content: '确定要删除选中的记录，删除后将删除该数据的所有隐患项？',
        onOk: () => {
          that.spinShow = true;
          that.axios.post(that.apiUrl.webApi + "/safety/risk/riskPlanDelete", {uuid: data.uuid}).then(res => {
            that.spinShow = false;
            if (!!res) {
              that.$Message.success('数据删除成功.');
              that.$refs.wzTbale.tableLoadData();
            }
          }).catch(err => {
            console.log("失败", err)
          })
        },
        onCancel: () => {
          //点击取消
        }
      });
    },
    addTableData() {//table添加
      var that = this;
      that.formTitle = "添加";
      that.selectData = {};
      that.formOperation = true;
    },
    searchSubmit() {//搜索
      this.formDataSearch.riskBeginDate = !!this.dateRange[0] ? this.public.timeFormatting(this.dateRange[0], "yyyy-MM-dd") : '';
      this.formDataSearch.riskEndDate = !!this.dateRange[1] ? this.public.timeFormatting(this.dateRange[1], "yyyy-MM-dd") : '';
      this.tableData.where = JSON.parse(JSON.stringify(this.formDataSearch));
      this.$refs.wzTbale.tableLoadData(true);
    },
    //添加修改触发后关闭刷新
    closeFun(data) {
      var that = this;
      if (data) {//添加修改 刷新表单
        that.$refs.wzTbale.tableLoadData();
      }
      that.formOperation = false;
      that.seeMoreType = false;
    },
    //跳转隐患排查
    hiddenPerils(data){
      this.$emit('hiddenPerils', data)
      this.screeningPlanDetailsType = false;
    }
  },
  watch: {//监听

  },
  beforeUpdate() {//beforeUpdate 更新前状态

  },
  updated() {//updated 更新完成状态

  },
  beforeDestroy() {//beforeDestroy 销毁前状态

  },
  destroyed() {//destroyed 销毁完成状态

  }
}
</script>
<style lang="less">
._wz_hiddenTroubleplan {
  display: flex;
  flex-direction: column;
}
</style>
